package com.baidu.process.chat.core.web.service.impl;

import com.baidu.process.chat.core.web.bean.eval.EvalResult;
import com.baidu.process.chat.core.web.bean.eval.R;
import com.baidu.process.chat.core.web.entity.EvalDTO;
import com.baidu.process.chat.core.web.entity.Evaluation;
import com.baidu.process.chat.core.web.entity.EvaluationDTO;
import com.baidu.process.chat.core.web.mapper.EvaluationMapper;
import com.baidu.process.chat.core.web.service.IEvalService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: lws
 * @date: 2022/12/15
 */
@Service
public class EvalServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements IEvalService {

    @Override
    public R getData(String sessionId) {
        Assert.isTrue(StringUtils.isNotEmpty(sessionId), "会话编码不能为空");
        // 查询满意度评价数据
        Evaluation evaluation = this.lambdaQuery()
                .eq(Evaluation::getSessionId, sessionId)
                .one();
        // 不为空返回查询到的数据
        if (!Objects.isNull(evaluation)) {
            EvalDTO evalDTO = new EvalDTO();
            evalDTO.setSolution(evaluation.getSessionSolution());
            evalDTO.setStarValue(evaluation.getStarValue());
            evalDTO.setSelectItems(evaluation.getSelectItems());
            evalDTO.setReason(evaluation.getReason());
            return R.ok(evalDTO);
        }
        return R.ok();
    }

    @Override
    public R getStatisticsData(String startDate, String endDate) {
        Wrapper<Evaluation> qw = Wrappers.lambdaQuery(new Evaluation())
                .ge(Evaluation::getCreateDate, startDate)
                .le(Evaluation::getCreateDate, endDate)
                .groupBy(Evaluation::getCreateDate);
        List<Map<String, Object>> totalList = this.getBaseMapper().getEveryDayTotal(qw);
        Map<Object, List<Map<String, Object>>> createDate = totalList.stream().collect(Collectors.groupingBy(t -> t.get("createDate")));
        return R.ok(createDate);
    }
}
